import {
	offiacc,
	memberUserInfo,
	marketList,
	userMarketList,
	applogin,
	oauth,
	login
} from '../../service/index.js'
import store from '../index.js';
import {
	removeParam,
	parseQuery
} from '@/utils/index.js'

import {
	TUILogin
} from '@tencentcloud/tui-core';

let vueVersion = 2;
// #ifdef VUE3
vueVersion = 3;
// #endif

export default {
	namespaced: true,
	state() {
		return {
			system: 'h5',
			osName: '',
			appVersion: '',
			token: '',
			userInfo: {},
			list: [],
			// setting: {},
			// redirect_uri: ''
		}
	},
	mutations: {
		changeToken(state, token) {
			state.token = token
		},
		changeUserInfo(state, userInfo) {
			state.userInfo = userInfo
		},
		changeList(state, list) {
			state.list = list
		},
		changeSystem(state, system) {
			state.system = system
		},
		changeOsName(state, osName) {
			state.osName = osName
		},
		changeAppVersion(state, appVersion) {
			state.appVersion = appVersion
		},
		// changeSetting(state, setting) {
		// 	state.setting = setting
		// },
		// changeRedirectUri(state, redirect_uri) {
		// 	state.redirect_uri = redirect_uri
		// }
	},
	actions: {
		async userinfoAction({
			commit,
			dispatch
		}, payload) {
			return new Promise(async (resolve, reject) => {
				try {
					console.log("===")
					const result = await memberUserInfo({})
					console.log(result)
					if (result.code === 200) {
						commit('changeUserInfo', {
							...result.data,
						})
						resolve()
					}
				} catch (e) {
					reject()
				}
			})
		},
		// 登录
		// async loginAction({
		// 	commit,
		// 	dispatch
		// }, payload) {
		// 	try {
		// 		const {
		// 			code,
		// 			appid,
		// 			content_id,
		// 			t,
		// 			redirect_uri
		// 		} = parseQuery()
		// 		if (this.state.login.userInfo.appid && this.state.login.userInfo.appid != appid) {
		// 			commit('changeToken', '')
		// 			commit('changeUserInfo', {})
		// 			commit('changeList', [])
		// 			commit('changeSetting', {})
		// 			commit('changeRedirectUri', '')
		// 			try {
		// 				uni.removeStorageSync('form');
		// 			} catch (e) {
		// 				// error
		// 			}
		// 		}
		// 		if (!this.state.login.token && this.state.login.system === 'h5') {
		// 			if (code) {
		// 				const res = await oauth({
		// 					code: code,
		// 					appid: appid,
		// 					content_id: content_id || ''
		// 				})
		// 				if (res.code === 200) {
		// 					commit('changeToken', res.data.api_token)
		// 					commit('changeUserInfo', {
		// 						...res.data,
		// 						appid: res.data.appid
		// 					})
		// 					commit('redirect_uri', redirect_uri || '')
		// 					if (res.data.is_disable === 1) {
		// 						const divElement = document.createElement('div');
		// 						divElement.innerHTML =
		// 							`<div style='position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgb(255, 255, 255); color: #383838; z-index: 99999; text-align: center; padding-top: 20%;'>无权访问</div>`;
		// 						document.body.appendChild(divElement);
		// 					} else {
		// 						if (window.location.origin != res.data.domain) {
		// 							// 跳转境外需要把境内state重置
		// 							commit('changeToken', '')
		// 							commit('changeUserInfo', {})
		// 							commit('changeList', [])
		// 							commit('changeSetting', {})
		// 							commit('changeRedirectUri', '')
		// 							try {
		// 								uni.removeStorageSync('form');
		// 							} catch (e) {
		// 								// error
		// 							}
		// 							let _href = `${res.data.domain}/user/${window.location.hash}`
		// 							_href = _href.replace(/appid=([^&]+)/, `appid=${res.data.appid}`)
		// 							_href = removeParam("state", _href)
		// 							_href = removeParam("code", _href)
		// 							window.location.href = _href
		// 						} else {
		// 							window.location.reload()
		// 						}
		// 					}
		// 				} else {
		// 					// 失败 进入预设页面
				
		// 				}
		// 			} else {
		// 				let http = `${window.location.href}`
		// 				http = removeParam("state", http)
		// 				http = removeParam("code", http)
		// 				// 如果是支付宝支付页面，不进行微信授权
		// 				if (window.location.href.indexOf("ali_pay") !== -1) {} else {
		// 					window.location.href =
		// 						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(http.replace("#", "virtually"))}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
		// 				}
		// 			}
		// 		}
		// 	} catch (e) {}
		// },
		// 登录
		async appLoginAction({
			commit,
			dispatch
		}, payload) {
			try {
				login(payload).then(res => {
					console.log(res)
					if (res.code == 200) {
						if (res.data.is_disable === 1) {
							return uni.showToast({
								title: '无权访问',
								icon: 'none'
							})
						}
						commit('changeToken', res.data.ApiToken)
						console.log(">>>")
						// 调用userInfo
						dispatch('userinfoAction').then(res => {
							uni.showToast({
								title: '登录成功',
								icon: 'none'
							})
							uni.redirectTo({
								url: "/pages/index/index",
								success: () => {
									
								},
							});
						}).catch(() => {});
						return
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			} catch (e) {}
		},
		// 退出登录
		loginOut({
			state,
			commit
		}) {
			commit('changeToken', '')
			commit('changeUserInfo', {})
			commit('changeList', [])
			// commit('changeSetting', {})
			// commit('changeRedirectUri', '')
			try {
				uni.removeStorageSync('form');
			} catch (e) {
				// error
			}
			uni.reLaunch({
				url: "/pages/index/index",
				success: () => {
					console.log("退出成功")
					setTimeout(() => {
						uni.showToast({
							title: '已退出',
							icon: 'none'
						})
					}, 0)
				},
			});
		},
	},
	getters: {
		roleId: state => state.userInfo.roleId,
		userId: state => state.userInfo.id,
		nickName: state => state.userInfo.nickName,
		roleName: state => state.userInfo.roleName,
		avatar: state => state.userInfo.avatar,
		phone: state => state.userInfo.phone,
	}
}